package com.lzp.copyui.draganddrop

import android.graphics.*
import android.util.Log
import android.view.View

/**
 * @description:
 * @author: lxp
 * @date: 2022/8/12 下午2:50
 */
class DragShadow(view: View) : View.DragShadowBuilder(view) {

    private val TAG = "DragShadow"

    private val bgMargin = 30f
    private val rectf by lazy {
        RectF().apply { set(-bgMargin, -bgMargin, view.width + bgMargin, view.height + bgMargin) }
    }

    private val bgPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG).apply {
            style = Paint.Style.FILL
            color = 0xff000000.toInt()
        }
    }

    override fun onProvideShadowMetrics(outShadowSize: Point?, outShadowTouchPoint: Point?) {
        super.onProvideShadowMetrics(outShadowSize, outShadowTouchPoint)
        if (outShadowTouchPoint == null) {
            return
        }
        if (outShadowSize == null) {
            return
        }
        if (view != null) {
            outShadowSize.set(rectf.width().toInt(), rectf.height().toInt())
            outShadowTouchPoint.set(outShadowSize.x / 2, outShadowSize.y / 2)
        } else {
            Log.e(TAG, "Asked for drag thumb metrics but no view")
        }

    }



    override fun onDrawShadow(canvas: Canvas?) {
        canvas?.translate(bgMargin,bgMargin)
        canvas?.drawRoundRect(rectf, 10f, 10f, bgPaint)
        super.onDrawShadow(canvas)
        //view.draw(canvas)
    }
}